package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfStatusEnum;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCWfCleaHdr;
import com.auxgroup.hr.front.dao.repository.PsCLeaveDefnVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCLeaveTypeVwRepository;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.WfCleaHdrService;
import com.auxgroup.hr.front.service.process.WfPlaveHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfCleaHdrReq;
import com.auxgroup.hr.front.site.vo.process.WfCleaHdrRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set;

/**
 * 销假申请CO
 * 
 * @author wangchangyun
 * @version 创建时间：180527
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfcleaHdr")
public class WfCleaHdrCO extends BaseWfCO {

	private static final Logger logger = LoggerFactory.getLogger(WfCleaHdrCO.class);

	@Autowired
	public WfPlaveHdrService wfPlaveHdrService;
	@Autowired
	public WfCleaHdrService wfCleaHdrService;
	
	@GetMapping(value="/getWfCleaHdr")
	public BaseResponseVO getWfCleaHdr(String cTransactionNbr,@SessionGain("user") UserDTO user){
		if(StringUtils.isBlank(cTransactionNbr)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		PsCWfCleaHdr laveHdr =  wfCleaHdrService.getWfInfo(cTransactionNbr);
		if(laveHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}

		WfCleaHdrRespVO respVO = objectMapper.map(laveHdr, WfCleaHdrRespVO.class);

		fillBaseWfInfo(user, laveHdr, respVO);
		respVO.setDtls(wfCleaHdrService.getDtls(cTransactionNbr));
		return buildRespone(respVO);
	}

	/**
	 * 创建休假流程
	 * @param user
	 * @return
	 */
	@PostMapping(value="/saveClea")
	public BaseResponseVO saveClea(@Valid @RequestBody WfCleaHdrReq orireq, @SessionGain("user") UserDTO user){
		logger.info("PrcHdr save param:{}",orireq);

		if(orireq.getTransNums() == null || orireq.getTransNums().isEmpty()){
			return buildRespone(BaseRetCode.CODE_ERROR_INPUT_NOT_RIGHT);
		}
		PsCWfCleaHdr cleaHdrReq = new PsCWfCleaHdr();
		cleaHdrReq.setcTransactionNbr(orireq.getcTransactionNbr());
		cleaHdrReq.setcWfEmplid(user.getEmplId());
		cleaHdrReq.setcCleaveType(orireq.getcCleaveType());
		cleaHdrReq.setcApplInfo(orireq.getcApplInfo());
		cleaHdrReq.setStartDate(orireq.getStartDate());
		cleaHdrReq.setEndDate(orireq.getEndDate());

		if (!fillBeforeSubmit(cleaHdrReq, user)) return buildRespone(BaseRetCode.CODE_ERROR_GET_TRANSACTION_NBR_FAILED);
        PsCWfCleaHdr laveHdr = wfCleaHdrService.saveWfInfo(cleaHdrReq, user);
		boolean dtlResult = wfCleaHdrService.saveDtls(laveHdr.getcTransactionNbr(), orireq.getTransNums());
		if(!dtlResult) {
			logger.error("流程申请出错，原流程信息不正确");
			return buildRespone(BaseRetCode.CODE_ERROR_INPUT_NOT_RIGHT);
		}

		if(laveHdr != null){
			return submitWf(user, WfTypeEnum.WF_STYPE_SEL.getType(), laveHdr);
//			if(!BaseRetCode.CODE_SUCCESS.getRetCode().equals(resp.getRetCode())
//					&& WfStatusEnum.WF_STATUS_S.getStatus().equals(laveHdr.getcWfStatus())){
////				wfCleaHdrService.delWfInfo(laveHdr.getcTransactionNbr());
//				logger.error("请假流程申请失败");
//			}
//			return resp;
		}else{
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_APPLY_FAIL);
		}
	}


	@Override
	protected Set<String> getAuthFileds() {
		return null;
	}
}
